package com.sduept.nwld.dataserver.controller.dataview.weather;

import com.sduept.bigdata.weather.entity.LdEquipment;
import com.sduept.bigdata.weather.manager.ThunderDataManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.utils.DateUtil;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;

@Named
@ViewScoped
public class ThunderDataController extends AbstractController {

	private static final long serialVersionUID = 8159894486511107010L;
	@Autowired
	private ThunderDataManager manager;

	List<LdEquipment> datas = new ArrayList<LdEquipment>();
	private LazyDataModel<LdEquipment> model;
	// 查询条件 开始时间
	private Date start;
	// 查询条件 结束时间
	private Date end;
	public int countRow = 0;

	@PostConstruct
	public void init() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.add(Calendar.DATE, -1);
		Date d = cal.getTime();
		start = d;
		end = Calendar.getInstance().getTime();
		query();
	}

	public void query() {
		model = new LazyDataModel<LdEquipment>() {

			private static final long serialVersionUID = 1265756074009804360L;

			@Override
			public List<LdEquipment> load(int first, int pageSize, String sortField, SortOrder sortOrder,
					Map<String, Object> filters) {
				datas = manager.getLdEquipmentByTime(start, end, new int[] { first, first + pageSize });
				return datas;
			}

			@Override
			public List<LdEquipment> load(int first, int pageSize, List<SortMeta> multiSortMeta,
					Map<String, Object> filters) {
				datas = manager.getLdEquipmentByTime(start, end, new int[] { first, first + pageSize });
				return datas;
			}

			@Override
			public LdEquipment getRowData(String rowKey) {
				for (LdEquipment t : datas) {
					if (t.getId().equals(rowKey)) {
						return t;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(LdEquipment object) {
				for (LdEquipment t : datas) {
					if (t.getId().equals(object.getId())) {
						return t;
					}
				}
				return null;
			}
		};
		end = DateUtil.getDateByString(DateUtil.getStringDate(end, DateUtil.pattern_ymd235959),DateUtil.pattern_ymdhms);
		long rowCount = manager.countByCondition(start, end);
		countRow = (int) rowCount;
		model.setRowCount(countRow);
	}

	public List<LdEquipment> getDatas() {
		return datas;
	}

	public void setDatas(List<LdEquipment> datas) {
		this.datas = datas;
	}

	public LazyDataModel<LdEquipment> getModel() {
		return model;
	}

	public void setModel(LazyDataModel<LdEquipment> model) {
		this.model = model;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public int getCountRow() {
		return countRow;
	}

	public void setCountRow(int countRow) {
		this.countRow = countRow;
	}

}
